package com
{
	import com.config.GameConfig;
	import com.connect.NetEvent;
	import com.connect.agreement.mclgc.ChatDataHandler;
	import com.connect.agreement.mclgc.Packet;
	import com.connect.java.JavaClient;
	import com.game.Group;
	import com.module.login.LoginManager;
	import com.module.mail.MailManager;
	import com.module.regrole.RegRoleManager;
	
	import flash.events.Event;
	
	import mx.controls.Alert;

	public class Connect
	{
		public function Connect()
		{
		}
		
		public static var server:JavaClient = new JavaClient();
		public static var chat:JavaClient = new JavaClient();
		
		public static function connect():void
		{
			server.addEventListener(Event.CONNECT,connectHandler);
			server.addEventListener(Event.CLOSE,closeHandler);
			server.addEventListener(NetEvent.SEND_PACKAGE,getPacketHandler);
			server.connect(GameConfig.instance.mainserver,GameConfig.instance.mainport);
			
			chat.addEventListener(Event.CONNECT,connectChatHandler);
			chat.addEventListener(Event.CLOSE,closeChatHandler);
			chat.addEventListener(NetEvent.SEND_PACKAGE,getChatPacketHandler);
			chat.handler = new ChatDataHandler();
			chat.connect(GameConfig.instance.chatserver,GameConfig.instance.chatport);
		}
		
		private static function connectHandler(event:Event):void
		{
			trace("connect to server!");
		}
		
		private static function closeHandler(event:Event):void
		{
			
		}
		
		private static function getPacketHandler(event:NetEvent):void
		{
			var p:Packet = event.data as Packet;
			p.data.uncompress();
			switch(p.group)
			{
				case Group.Login:
					LoginManager.instance.update(p);
					break;
				case Group.RegRole:
					RegRoleManager.instance.update(p);
					break;
				case Group.Mail:
					MailManager.instance.update(p);
					break;
				case Group.Error:
					ErrorManager.update(p);
					break;
			}
		}
		
		private static function connectChatHandler(event:Event):void
		{
			trace("connect to chat server!");
		}
		
		private static function closeChatHandler(event:Event):void
		{
			
		}
		
		private static function getChatPacketHandler(event:Event):void
		{
			
		}
		
	}
}